<?php

/**
 * Invitaion Response entity.
 * @author james
 *
 */
class InvitationResponse extends Zend_Db_Table_Abstract { 
	protected $_name = 'invitation_response';
	protected $_primary = 'id';

	protected $_referenceMap = array(
		'Invitation' => array(
			'columns' => 'invitation_id',
			'refTableClass' => 'Invitation',
			'refColumns' => 'id'
		),
		'User' => array(
			'columns' => 'invited_user_id',
			'refTableClass' => 'User',
			'refColumns' => 'id'
		)
	);
	
	/**
	 * Find by id.
	 * 
	 * @param $id
	 * @return return invitation response
	 */
	static function findById($id) {
		$invitationResponse = new InvitationResponse();
		return $invitationResponse->find($id)->current();
	}
	
	/**
	 * Find unique.
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 */
	function findUnique($invitation_id, $invited_user_id) {
		$where = $this->select()
		->where('invitation_id = ?', $invitation_id)
		->where('invited_user_id = ?', $invited_user_id);
		return $this->fetchRow($where);
	}
	
	/**
	 * Add invitation response.
	 * 
	 * @param $invitation_id
	 * @param $invited_user_id
	 * @param $response
	 * @return return invitation response id
	 */
	function addResponse($invitation_id, $invited_user_id, $response) {
		$sql_date_pattern = 'yyyy-MM-dd HH:mm:ss';
		$date_obj = new Zend_Date();
		$current_time = $date_obj->get($sql_date_pattern);
		$data = $this->findUnique($invitation_id, $invited_user_id);
		if ($data) {
			$data->response = $response;
			return $data->save();
		} else {
			$data = $this->createRow();
			$data->invitation_id = $invitation_id;
			$data->invited_user_id = $invited_user_id;
			$data->response = $response;
			$data->created = $current_time;
			return $data->save();
		}
		
	}
	
	/**
	 * Get user.
	 * 
	 * @param $id invitation response
	 */
	public static function getUser($invitationResponse) {
		return $invitationResponse->findParentRow('User');
	}
}
?>